<template>
    <el-container>
        <el-aside width="15%"></el-aside>
        <el-main>
            <el-alert title="这是管理员登录界面，请游客和普通用户止步" type="warning" show-icon></el-alert>
            <el-divider></el-divider>
            <!--表单-->
            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" type="text"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
            <el-divider></el-divider>
            已登录？进入
            <el-link type="primary" @click="$router.push('/admin')">管理员页面</el-link>
        </el-main>
        <el-aside width="15%"></el-aside>
    </el-container>
</template>

<script>
import axios from 'axios'
export default {
    name: 'Login',
    data() {
        return {
            ruleForm: {
                username: null,
                password: null
            },
            rules: {
                username: [
                    {required: true, message: '请输入用户名称', trigger: 'blur'},
                    {min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入用户名称', trigger: 'blur'},
                    {min: 4, message: '长度在 4 个字符以上', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.login(this.ruleForm.username, this.ruleForm.password);
                } else {
                    this.showMessage('warning', '输入信息不合法');
                    return false;
                }
            });
        },
        resetForm (formName) {
            this.$refs[formName].resetFields();
        },
        login (username, password) {
            axios({
                method: "post",
                url: "/admin/login",
                data: {
                    username: username,
                    password: password
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then((response) => {
                if (response.data.flag) {
                    this.$router.replace("/admin");
                } else {
                    this.showMessage('error', response.data.msg);
                }
            }, (err) => {
                console.warn(err);
            });
        },
        showMessage (type, message) {
            this.$message({
                message: message,
                type: type
            });
        }
    },
    mounted() {
        //如果已登录，直接跳转到管理员页面
        axios.post('/admin/checkLogin').then(reponse => {
            if (reponse.data.flag && reponse.data.res.status == 1) {
                //已登录管理员，可以进去
                this.$router.replace('/admin')
            } else{
                console.log('请登录')
            }
        }, err => {
            console.warn(err)
        })
    }
}
</script>

<style scoped>
    .el-aside {
        background-color: whitesmoke;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        background-color: #E9EEF3;
        color: #333;
        text-align: center;
        line-height: 60px;
        padding-top: 0px;
        padding-bottom: 0px;
        overflow: hidden;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }
</style>